#include "FLOAT.h"



 
FLOAT F_mul_F(FLOAT a, FLOAT b) {
  return (FLOAT)(((long long)a * (long long)b) >> 16);
}

FLOAT F_div_F(FLOAT a, FLOAT b) {
    FLOAT quotient, remainder; 
    asm volatile("idiv %2":"=a"(quotient),"=d"(remainder):"r"(b),"a"(a<<16),"d"(a>>16));
    return quotient;
}



 
#define __sign(x) ((x) & 0x80000000)
 
FLOAT f2F(float a) {
  

  int b = *(int*)&a;
  int sign = b >> 31;
  int exp = (b >> 23) & 0xff;
  int x = b & 0x7fffff;
  if (exp != 0)
    x += 1 << 23;
  exp -= 150;
  if (exp < -16)
    x >>= -16 - exp;
  if (exp > -16)
    x <<= 16 + exp;
  return sign == 0 ? x : -x;
}


FLOAT Fabs(FLOAT a) {

	return a < 0 ? -a : a;
}

/* Functions below are already implemented */

FLOAT sqrt(FLOAT x) {
	FLOAT dt, t = int2F(2);

	do {
		dt = F_div_int((F_div_F(x, t) - t), 2);
		t += dt;
	} while(Fabs(dt) > f2F(1e-4));

	return t;
}

FLOAT pow(FLOAT x, FLOAT y) {
	/* we only compute x^0.333 */
	FLOAT t2, dt, t = int2F(2);

	do {
		t2 = F_mul_F(t, t);
		dt = (F_div_F(x, t2) - t) / 3;
		t += dt;
	} while(Fabs(dt) > f2F(1e-4));

	return t;
}

